<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.Receiveorder"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Project"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	ProjectDAO projectDao = (ProjectDAO)ctx.getBean("ProjectDAO");
	
	String ruCode = request.getParameter("ruCode");
	String wareDate = request.getParameter("wareDate");
	String reqSupName = request.getParameter("reqSupName");
	String recCompany = request.getParameter("recCompany");
	String tobId = request.getParameter("tobId");
	String subStore = request.getParameter("subStore");
	String projectId = request.getParameter("projectId");
	String remark = request.getParameter("remark");
	
	int uid = (Integer)session.getAttribute("id");
	int tid = (Integer)session.getAttribute("tid");
	
	try{
		String proCode = "";
	    if(projectId != null && !"".equals(projectId.trim())){
	    	Project project = projectDao.findById(Integer.parseInt(projectId));
	    	proCode = project.getCode();
	    }
		
		Receiveorder receiveorder = new Receiveorder();
		receiveorder.setCode("");
		receiveorder.setDate(new Date());
		receiveorder.setIsDelete(0);
		receiveorder.setProCode(proCode);
		//receiveorder.setRcode(ruCode);
		receiveorder.setReceiveCompany(recCompany);
		receiveorder.setReceiverDate(new SimpleDateFormat("yyyy-MM-dd").parse(wareDate));
		receiveorder.setReceiverUserId(uid);
		receiveorder.setRemark(remark);
		receiveorder.setSubStore(Integer.parseInt(subStore));
		receiveorder.setSupplier(reqSupName);
		receiveorder.setTeamId(tid);
		receiveorder.setTopType(tobId);
		receiveorderDao.save(receiveorder);
		
		receiveorder.setRcode("R"+receiveorder.getId());
		receiveorderDao.attachDirty(receiveorder);
		
		String[] materialIds = request.getParameterValues("materialId");
		String[] batchs = request.getParameterValues("batch");
		String[] places = request.getParameterValues("placeId");
		String[] orderNums = request.getParameterValues("orderMatNum");
	    String[] numbers = request.getParameterValues("reqMatNum");
	    String[] prices = request.getParameterValues("reqMatPrice");
	    
		for(int i=0;i<materialIds.length;i++){
        	Receivepurmat receivepurmat = new Receivepurmat();
        	receivepurmat.setBatch(batchs[i]);
        	receivepurmat.setDate(new Date());
        	receivepurmat.setImportNum(Integer.parseInt(numbers[i]));
        	receivepurmat.setStoreNum(Integer.parseInt(numbers[i]));
        	receivepurmat.setIsDelete(0);
        	receivepurmat.setMaterialId(Integer.parseInt(materialIds[i]));
        	receivepurmat.setPlaceId(Integer.parseInt(places[i]));
        	System.out.println("----------------" + proCode);
        	receivepurmat.setProCode(proCode);
        	receivepurmat.setPurNum(Integer.parseInt(orderNums[i]));
        	receivepurmat.setPurPrice(Float.parseFloat(prices[i]));
        	receivepurmat.setReceiveOrderId(receiveorder.getId());
        	receivepurmat.setSubStore(Integer.parseInt(subStore));
        	receivepurmat.setTeamId(tid);
        	receivepurmat.setUseNum(Integer.parseInt(numbers[i]));
        	receivepurmat.setUserId(uid);
        	receivepurmatDao.save(receivepurmat);
        }
		
		request.setAttribute("info","success");
	}catch(Exception e){
		e.printStackTrace();
		request.setAttribute("info","error");
	}
    
	pageContext.forward("/vm/stock/addStock.vm");
%>